﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Semantics.Silverlight;
using DanielVaughan;
namespace SilverlightParser
{
    public partial class NodeDialog : ChildWindow
    {
        public string Word;
        public int CategoryNodeId = -1;
        public NodeDialog(string word)
        {
            this.Word = word;
            InitializeComponent();
            this.categoryControl1.SelectedCategoryChanged+=new CategoryControl.CategoryEventHandler(categoryControl1_SelectedCategoryChanged);
        }

        Window Owner
        {
            get
            {
                object parent = this.Parent;
                while (!(parent is Window))
                    parent = ((Control)parent).Parent;
                return (Window)parent;
            }
        }
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.categoryControl1.SelectedTreeViewItem == null)
            {
                MessageBox.Show("没有选择节点!");
            }
            this.CategoryNodeId = this.categoryControl1.SelectedCategory.NodeId;
            this.DialogResult = true;
            this.Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void ChildWindow_Closed(object sender, EventArgs e)
        {
            this.Mentor.ExceptionEvent -= this.Mentor_ExceptionEventHanlerDelegate;
        }
        RemotableMentorProxy Mentor = new RemotableMentorProxy();
        ExceptionHandlerDelegate Mentor_ExceptionEventHanlerDelegate;
        private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                Mentor_ExceptionEventHanlerDelegate = new ExceptionHandlerDelegate(Mentor_ExceptionEvent);
                this.Mentor.ExceptionEvent += Mentor_ExceptionEventHanlerDelegate;
                this.exceptiondelegate=new ExceptionHandlerDelegate(this.CrossThreadException);
                this.textBoxNodeText.Text = this.Word;
                this.categoryControl1.ShowRootCategory(Mentor);
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString());
            }
        }


        void CrossThreadException(Exception exp)
        {
            MessageBox.Show(exp.ToString());
        }
        ExceptionHandlerDelegate exceptiondelegate;
        void Mentor_ExceptionEvent(Exception exp)
        {
            try
            {
                this.Dispatcher.BeginInvoke(exceptiondelegate, exp);
            }
            catch (Exception exp2)
            {
            }
        }

        private void buttonSearch_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.textBoxWordClassToSearch.Text = this.textBoxWordClassToSearch.Text.Trim();
                if (this.textBoxWordClassToSearch.Text.Length == 0)
                {
                    MessageBox.Show("请输入词语!");
                    return;
                }
                string word = this.textBoxWordClassToSearch.Text;
                System.Threading.ThreadPool.QueueUserWorkItem(delegate
                {
                    DoSearch(word);
                });
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString());
            }
        }

        void DoSearch(string word)
        {
            List<Category> cates = this.Mentor.SearchCategories(word);
            this.Dispatcher.BeginInvoke(delegate{
                    ShowCategories(cates);
                });
        }

        void ShowCategories(List<Category> categories)
        {
            if (categories.Count == 0)
            {
                MessageBox.Show("找不到词语'" + this.textBoxWordClassToSearch.Text + "'");
                return;
            }
            if (categories.Count == 1)
            {
                Category cate = categories[0];
                if(cate.Example!=null)
                    this.textBoxSimilarWord.Text = cate.Example;
                this.categoryControl1.ApplyCategory(cate);
            }
        }

        public Category SelectedCategory;
        private void categoryControl1_SelectedCategoryChanged(Category category)
        {
            this.textBoxSelectedCategory.Text = category.ToString();
            this.SelectedCategory = category;
        }
    }
}

